/**
  *提现
  */
module game {

    export class TiXianMediator extends BaseMediator {
        public static NAME: string = "TiXianMediator";
        private obj;
        public constructor(viewComponent: any = null) {
            super(TiXianMediator.NAME, viewComponent);
        }

        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.HALL.OPEN_TIXIAN,
                PanelNotify.HALL.CLOSE_TIXIAN
            ];
        }
        private panel: TiXianPanel;
        private yibangding = false
        private BankAccountUsername = ""
        public handleNotification(notification: puremvc.INotification): void {
            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.panel) {
                        this.panel.width = GameConfig.curWidth()
                        this.panel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.HALL.OPEN_TIXIAN: {
                    this.obj = notification.getBody();
                    this.panel = null;
                    this.panel = new TiXianPanel();
                    this.showUI(this.panel, 2, 1, true);
                    break;
                }
                case PanelNotify.HALL.CLOSE_TIXIAN: {
                    this.closePanel(0);
                    break;
                }
            }
        }

        /**
         * 初始化面板ui
         */
        public initUI(): void {
            this.panel.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeButtonClick, this);
            this.panel.duihuanjilu.addEventListener(egret.TouchEvent.TOUCH_TAP, this.duihuanjiluClick, this);
            this.panel.duihuanguize.addEventListener(egret.TouchEvent.TOUCH_TAP, this.duihuanguizeClick, this);
            this.panel.qingchu.addEventListener(egret.TouchEvent.TOUCH_TAP, this.qingchuClick, this);
            this.panel.conmitButton.addEventListener(egret.TouchEvent.TOUCH_TAP, this.conmitButtonClick, this);
            this.panel.yinhangkahao.addEventListener(egret.TouchEvent.TOUCH_TAP, this.bangDingYinHang, this);
            this.panel.genghuan.addEventListener(egret.TouchEvent.TOUCH_TAP, this.gengHuanYinHang, this);
            this.panel.moneyBtn1.addEventListener(egret.TouchEvent.TOUCH_TAP, this.moneyBtnClick, this);
            this.panel.moneyBtn2.addEventListener(egret.TouchEvent.TOUCH_TAP, this.moneyBtnClick, this);
            this.panel.moneyBtn3.addEventListener(egret.TouchEvent.TOUCH_TAP, this.moneyBtnClick, this);
            this.panel.moneyBtn4.addEventListener(egret.TouchEvent.TOUCH_TAP, this.moneyBtnClick, this);

            this.panel.moneyNum.addEventListener(egret.FocusEvent.FOCUS_IN, () => {
                this.panel.moneyNum.text = ""
            }, this)
            this.panel.moneyNum.addEventListener(egret.FocusEvent.FOCUS_OUT, () => {
                if (!RegUtils.isInteger(this.panel.moneyNum.text)) {
                    this.panel.moneyNum.text = ""
                    EffectUtils.textFadeAway("请输入正确的数值！")
                    return;
                }
                var text = parseInt(this.panel.moneyNum.text)
                if (text < 100 || text % 50 != 0) {
                    this.panel.moneyNum.text = ""
                    EffectUtils.textFadeAway("最低起兑100元，且必须是50的倍数！")
                    return;
                }
            }, this)
        }

        /**
         * 初始化面板数据
         */
        public initData(): void {
            this.panel.yinhangkahao.touchEnabled = false
            this.panel.xiedaijine.text = GameVO.getVo().lUserScore / 100 + ""
            //查询银行
            var time = Math.floor(Global.getCurrTimeLong() / 1000)//这个逼接口时间是以秒做单位
            var signature = Global.getSynLoginKey(time)
            signature = signature.toUpperCase()
            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + "/GameWebApplication/PayBindCardRequest.ashx?userid=" + GameVO.getVo().dwUserID + "&time=" + time + "&signature=" + signature, egret.HttpMethod.POST);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                EffectUtils.hideWaritPanel()
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                    if (obj.code == 0) {
                        this.findBank(obj)
                    } else {
                        Global.tipShow(obj.msg)
                    }
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
                //查询打码量
                var time2 = Math.floor(Global.getCurrTimeLong() / 1000)//这个逼接口时间是以秒做单位
                var signature2 = Global.getSynLoginKey(time2)
                var request2 = new egret.HttpRequest();
                request2.responseType = egret.HttpResponseType.TEXT;
                request2.open(InstructionsEnum.IP_API + "/GameWebApplication/Api/WithdrawSetting.ashx?userid=" + GameVO.getVo().dwUserID + "&time=" + time2 + "&signature=" + signature2, egret.HttpMethod.POST);
                request2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                request2.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                    try {
                        var request2 = <egret.HttpRequest>event.currentTarget;
                        var obj = JSON.parse(request2.response)
                        if (obj.code == 0) {
                            this.findDamaliang(obj)
                        } else {
                            Global.tipShow(obj.msg)
                        }
                    } catch (err) {
                        EffectUtils.textFadeAway("数据包异常，无法解析!")
                    }
                }, this);
                request2.send();
            }, this);
            request.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                EffectUtils.hideWaritPanel()
                Global.tipShow("网络请求失败，请稍后重试!")
            }, this);
            EffectUtils.showWaritPanel()
            request.send();

        }
        //查询绑定的银行号返回
        private findBank(obj: any) {
            var Banks: Array<any> = obj["Banks"]
            if (Banks.length == 0) {
                this.panel.yinhangkahao.text = "点击绑定"
                this.panel.yinhangkahao.touchEnabled = true
                this.panel.genghuan.visible = false
                this.yibangding = false
            } else {
                this.panel.yinhangkahao.touchEnabled = false
                this.panel.yinhangkahao.italic = false
                this.panel.yinhangkahao.textColor = 0xF4CC6E
                var obj = Banks[0];
                this.BankAccountUsername = obj["BankAccountUsername"]
                var Bank = obj["Bank"]
                var AccountNo: string = obj["AccountNo"];
                this.panel.yinhangkahao.text = AccountNo.substring(0, 6) + "******" + AccountNo.substring(AccountNo.length - 6);
                this.panel.genghuan.visible = true
                this.yibangding = true
            }
        }
        //查询打码量返回
        private findDamaliang(obj: any) {
            var damaliang = obj["damaliang"]
            this.panel.damaliang.text = damaliang / 100 + ""
        }
        //关闭界面
        private closeButtonClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            this.closePanel(1);
        }
        //点击兑换记录
        private duihuanjiluClick() {
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_TIXIANJILU)
        }
        //点击兑换规则
        private duihuanguizeClick() {
            Global.tipShow("兑换5-10分钟到账\r如未到账请及时联系客服\r兑换金额需为50的倍数，最低100")
        }
        //清除金额输入框
        private qingchuClick() {
            this.panel.moneyNum.text = "";
        }
        //提交表单
        private conmitButtonClick() {
            if (this.yibangding == false) {
                Global.tipShow("请先绑定银行卡！")
            } else {
                var money = this.panel.moneyNum.text;
                if (!RegUtils.isInteger(money)) {
                    EffectUtils.textFadeAway("请输入兑换金额")
                    return;
                }
                var time = Global.getCurrTimeLong()
                var signature = Global.getSynLoginKey(time)
                signature = signature.toUpperCase()
                var request = new egret.HttpRequest();
                request.responseType = egret.HttpResponseType.TEXT;
                request.open(InstructionsEnum.IP_API + "/GameWebApplication/PayMoneyChange.ashx?userid=" + GameVO.getVo().dwUserID + "&signature=" + signature + "&time=" + time, egret.HttpMethod.POST);
                request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                    EffectUtils.hideWaritPanel()
                    try {
                        var request = <egret.HttpRequest>event.currentTarget;
                        var obj = JSON.parse(request.response)
                        if (obj.code == 0) {
                            game.AppFacade.instance.sendNotification(PanelNotify.HALL.FIND_USER_MONEY)
                            this.closePanel(1)
                            Global.tipShow("申请成功!请等待审核")
                        } else {
                            Global.tipShow(obj.msg)
                        }
                    } catch (err) {
                        EffectUtils.textFadeAway("数据包异常，无法解析!")
                    }
                }, this);
                request.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                    EffectUtils.hideWaritPanel()
                    Global.tipShow("网络请求失败，请稍后重试!")
                }, this);
                var obj = {
                    Money: Number(money),
                    UserId: GameVO.getVo().dwUserID,
                    BankAccountType: 0
                }
                EffectUtils.showWaritPanel()
                request.send(JSON.stringify(obj));
            }
        }
        //打开绑定银行卡
        private bangDingYinHang() {
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_BANK_CARD_BIND)
        }
        //打开更换银行卡
        private gengHuanYinHang() {
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_BANK_CARD_BIND_MODIFU, this.BankAccountUsername)
        }
        private moneyBtnClick(eve: egret.TouchEvent) {
            if (eve) {
                this.panel.moneyNum.text = eve.currentTarget.name;
            }
        }
    }
}